package com.example.learnx.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.learnx.model.entity.ExcelEntity;
import com.example.learnx.service.ExcelService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class ExcelServiceImpl implements ExcelService {
    @Override
    public List<ExcelEntity> getExcelByOneList(String fileAddress) {

        List<ExcelEntity> excelEntities = new ArrayList<>();

        //使用EasyExcel表格方法读取数据。
        //read方法里三个参数 文件名 模板类 一个解析监听
        //注意：监听里面的方法是需要我们重写的。
        EasyExcel.read(fileAddress, ExcelEntity.class, new AnalysisEventListener<ExcelEntity>() {

            //解析一行运行一次此方法。(注意舍弃第一行)
            @Override
            public void invoke(ExcelEntity excelEntity, AnalysisContext analysisContext) {
               excelEntities.add(excelEntity);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println(excelEntities.size());
            }
        }).sheet().doRead();

        return excelEntities;
    }
}
